package com.hisoft.web.view.report;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
public class ITextCell {
	private PdfPTable table;

	private Font fontNormal;

	private DecimalFormat df;

	public ITextCell(PdfPTable table) throws Exception {
		this.table = table;
		BaseFont bfChinese = BaseFont.createFont("STSong-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		fontNormal = new Font(bfChinese, 12, Font.NORMAL);

		df = (DecimalFormat) NumberFormat.getInstance();
		df.applyPattern("0.00");
		df.setMaximumFractionDigits(2);
	}

	public void creatCell(Object value) {
		if (null == value) {
			table.addCell(new PdfPCell(new Paragraph("")));
			return;
		}
		PdfPCell cell = null;
		if (value instanceof Integer) {
			cell = new PdfPCell(new Paragraph((Integer) value + ""));
			cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			table.addCell(cell);
		} else if (value instanceof Long) {
			cell = new PdfPCell(new Paragraph((Long) value));
			cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			table.addCell(cell);
		} else if (value instanceof Float) {
			cell = new PdfPCell(new Paragraph(df.format((Float) value)));
			cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			table.addCell(cell);
		} else if (value instanceof Double) {
			cell = new PdfPCell(new Paragraph(df.format((Double) value)));
			cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			table.addCell(cell);
		} else if (value instanceof BigDecimal) {
			cell = new PdfPCell(new Paragraph(df.format((BigDecimal) value)));
			cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			table.addCell(cell);
		} else if (value instanceof Date) {
			Date date = (Date) value;
			cell = new PdfPCell(new Paragraph(
					new SimpleDateFormat("yyyy-MM-dd").format(date)));
			cell.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			table.addCell(cell);
		} else {
			cell = new PdfPCell(new Paragraph((String) value, fontNormal));
			table.addCell(cell);
		}
	}
}
